import React, { useState } from 'react';

import { demoUtil } from '../../utils';

import {
  ResourceManager,
  columnFieldsMap,
  formFieldsMap,
  ResourceManagerProps,
} from 'easyComponent';

import { commonData } from './commonData';

export default () => {
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  const [selectedRows, setSelectedRows] = useState([]);
  const config: ResourceManagerProps = {
    componentsMap: { ...columnFieldsMap, ...formFieldsMap },
    rowKey: 'id',
    title: '用户管理',
    features: {
      rowSelection: {
        fixed: true,
        onChange: (_selectedRowKeys, _selectedRows) => {
          setSelectedRowKeys(_selectedRowKeys);
          setSelectedRows(_selectedRows);
        },
        infoRender: (_selectedRowKeys, _selectedRows) => {
          return `已选中：${_selectedRows?.reduce((prev, item) => {
            return prev + (prev ? '、' : '') + item.name;
          }, '')}`;
        },
      },
    },
    fields: [
      {
        label: '姓名',
        name: 'name',
        componentName: 'TextColumnField',
      },
      {
        label: '操作',
        name: 'name',
        componentName: 'OperationColumnField',
        actions: [
          {
            label: '编辑',
            handler: () => {
              demoUtil.toastInfo('你点击了编辑');
            },
          },
        ],
      },
    ],
    actions: [
      {
        label: '批量删除',
        position: 'RowSelection',
        handler: () => {
          console.log(selectedRows);
        },
      },
      {
        label: '批量确认',
        position: 'RowSelection',
        handler: () => {
          console.log(selectedRows);
        },
      },
    ],
  };

  return (
    <div>
      <ResourceManager
        {...config}
        dataSource={commonData.simpleDataSource}
        selectedRows={selectedRows}
        selectedRowKeys={selectedRowKeys}
      />
    </div>
  );
};
